CURRENT_PATH := $(shell pwd)
BUILD_DIR := $(CURRENT_PATH)/build
ifndef DORIS_VERSION
DORIS_VERSION := $(shell bash $(CURRENT_PATH)/get-latest-doris-version.sh)
endif
DEFAULT_VERSION := x.y.z
SRC_PKG_NAME := doris-service-mpack
DEST_PKG_DIR := $(BUILD_DIR)/$(SRC_PKG_NAME)

.PHONY:all

all: clean package
	@echo "completed"

prepare: prepare-build prepare-doris
	sed -s -i 's#$(DEFAULT_VERSION)#$(DORIS_VERSION)#g' $(DEST_PKG_DIR)/mpack.json
	@echo "prepare completed"

prepare-build:
	mkdir -p $(BUILD_DIR)
	cp -rf $(SRC_PKG_NAME) $(BUILD_DIR)

prepare-doris:
	mv -v $(DEST_PKG_DIR)/addon-services/NDDORIS/$(DEFAULT_VERSION) $(DEST_PKG_DIR)/addon-services/NDDORIS/$(DORIS_VERSION)
	mv -v $(DEST_PKG_DIR)/common-services/NDDORIS/$(DEFAULT_VERSION) $(DEST_PKG_DIR)/common-services/NDDORIS/$(DORIS_VERSION)
	xmlstarlet ed --inplace -u /metainfo/services/service/version -v $(DORIS_VERSION) -u /metainfo/services/service/extends -v common-services/NDDORIS/$(DORIS_VERSION) $(DEST_PKG_DIR)/addon-services/NDDORIS/$(DORIS_VERSION)/metainfo.xml
	xmlstarlet ed --inplace -u /metainfo/services/service/version -v $(DORIS_VERSION) -u /metainfo/services/service/osSpecifics/osSpecific/packages/package/name -v elasticsearch-$(DORIS_VERSION) $(DEST_PKG_DIR)/common-services/NDDORIS/$(DORIS_VERSION)/metainfo.xml

package: prepare
	cd $(BUILD_DIR) && tar zcf $(BUILD_DIR)/$(SRC_PKG_NAME).tar.gz $(SRC_PKG_NAME)
	@echo "package completed"

clean:
	@rm -rf $(BUILD_DIR)

help:
	@echo "do make build release mpack tar"
	@echo "do DORIS_VERSION=custom version build release mpack tar"
